package com.winning.rabbitmq.ttl;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.winning.rabbitmq.ConnectionUtils;
import com.winning.utils.IOUtils;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class TTLProducer {
    private static final String QUEUE_NAME = "TEST_TTL_QUEUE";

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;

        try{

            connection = ConnectionUtils.getConnection();
            channel = connection.createChannel();
            Map<String, Object> map = new HashMap<>();
            map.put("x-message-ttl", 6000);
            String msg = "Hello RabbitMQ";
            channel.queueDeclare(QUEUE_NAME, false, false, false, map);
            Hashtable hashTable = new Hashtable<>();
            hashTable.put("","");

            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                    .deliveryMode(2)//持久化消息
                    .contentEncoding("UTF-8")
                    .expiration("10000")
                    .build();
            channel.basicPublish("", "TEST_DLX_QUEUE", false, false, properties, msg.getBytes());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            IOUtils.close(channel);
            IOUtils.close(connection);
        }
    }

}
